﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Tebbee.UI.Client.ExchangeSer;

namespace Tebbee.UI.Client.Controllers.AdminApi
{
    public class AdminOrderController : ApiController
    {
        ExchangeSerClient db = new ExchangeSerClient();

        public IEnumerable<Tebbee.UI.Client.ExchangeSer.MemberOrderViewModel> GetOrders(int id = 0)
        {
            var queryString = HttpUtility.ParseQueryString(Request.RequestUri.Query);
            if (id < 1) id = 1;
            id = (id - 1) * 10;
            var tmp = db.ListAllOrder(id, queryString["value"], queryString["date"], int.Parse(queryString["status"].ToString()), int.Parse(queryString["type"].ToString()));
            return tmp;
        }

        [HttpGet]
        public MemberFull GetMember(string username)
        {
            return db.GetMemberByUsername(username);
        }

        public HttpResponseMessage PutOrder(MemberOrderViewModel order)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            String error = "";
            error = db.UpdateOrderStatus(order);
            if (error == "")
            {
                return Request.CreateResponse(HttpStatusCode.OK, order);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, error);
            }
        }
    }
}
